package com.general.genericUtils;

import java.util.Date;
import java.util.Iterator;
import java.util.Properties;

import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.apache.log4j.Logger;

public class SendMail {
	
	private  String mailTransportProtocol="smtp";
	private  String mailSmtpAuth="true";
	private  String mailUser="contact@allibilli.info";
	private  String mailPassword="gopikrishna";
	private  String mailSmtpHost="smtpout.secureserver.net";
	private  String mailFrom="contact@allibilli.info";
	private  String mailBcc="";
	private  String mailSubject="";
	private  String mailMessage="";
	private  String mailPort="";
	private  boolean mailDebug=false;
	
	private static Logger log = Logger.getLogger(SendMail.class);
	public  void sendEmail(EmailDetails emailDetails) {
		try{
		Properties props = System.getProperties();
		props.setProperty("mail.transport.protocol", getMailTransportProtocol());
		props.setProperty("mail.host", getMailSmtpHost());
		props.put("mail.smtp.auth", getMailSmtpAuth());
		props.setProperty("mail.user", getMailUser());
		props.setProperty("mail.password", getMailPassword());

		Session mailSession = Session.getDefaultInstance(props, null);
		mailSession.setDebug(isMailDebug());
		Transport transport = mailSession.getTransport(getMailTransportProtocol());
		MimeMessage message = new MimeMessage(mailSession);
		message.setSentDate(new Date());
		
		if(emailDetails.getSubject()!=null && emailDetails.getSubject().length()>0)
		{
			message.setSubject(emailDetails.getSubject());
		}
		else
		{
			message.setSubject(getMailSubject());
		}
		
		if(emailDetails.getFrom()!=null && emailDetails.getFrom().length()>0)
		{
			message.setFrom(new InternetAddress(emailDetails.getFrom()));
		}
		else
		{
			message.setFrom(new InternetAddress(getMailFrom()));
		}
		
		if(emailDetails.getTo()!=null && emailDetails.getTo().size()>0)
		for(Iterator<String> it = emailDetails.getTo().listIterator();it.hasNext();)
		{
			String to = (String)it.next();
			message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
			log.info(to);
		}
		
		if(emailDetails.getCc()!=null && emailDetails.getCc().size()>0)
		for(Iterator<String> it = emailDetails.getCc().listIterator();it.hasNext();)
		{
			message.addRecipient(Message.RecipientType.CC, new InternetAddress((String)it.next()));
		}
		if(emailDetails.getBcc()!=null && emailDetails.getBcc().size()>0)
		for(Iterator<String> it = emailDetails.getBcc().listIterator();it.hasNext();)
		{
			message.addRecipient(Message.RecipientType.BCC, new InternetAddress((String)it.next()));
		}
		if(getMailBcc().length()>0)
		{
			message.addRecipient(Message.RecipientType.BCC, new InternetAddress(getMailBcc()));
		}
		
		MimeMultipart multipart = new MimeMultipart("related");
		BodyPart messageBodyPart = new MimeBodyPart();
		messageBodyPart.setContent(getGenericEmailContent(emailDetails), "text/html");
		multipart.addBodyPart(messageBodyPart);
		message.setContent(multipart);
		transport.connect("smtpout.secureserver.net", "contact@allibilli.info","gopikrishna");
		transport.sendMessage(message, message
				.getRecipients(Message.RecipientType.TO));
		transport.close();
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
	public String getGenericEmailContent(EmailDetails emailDetails)
	{
		StringBuffer sb = new StringBuffer();
		sb.append("<Table Border=\"0\" CellSpacing=\"0\" CellPadding=\"0\"><Tr><Td Width=\"19\" Height=\"6\"></Td><Td Width=\"557\" Height=\"6\"></Td></Tr><Tr><Td Width=\"19\" Height=\"352\"></Td><Td Width=\"557\" Height=\"352\"><Table Border=\"0\" CellSpacing=\"0\" CellPadding=\"0\" style=\"font-family: Tahoma; font-size: 10pt\" ><Tr><Td Width=\"18\" Height=\"1\"><Img Src=\"http://ilc.allibilli.info/images/untitled1pane1_1_.jpg\" Border=\"0\" Height=\"1\" Width=\"18\" Name=\"Untitled1\" Alt=\"\"></Td><Td Width=\"152\" Height=\"1\"><Img Src=\"http://ilc.allibilli.info/images/untitled1pane2_1_.jpg\" Border=\"0\" Height=\"1\" Width=\"152\" Name=\"Untitled11\" Alt=\"\"></Td><Td Width=\"368\" Height=\"1\"><Img Src=\"http://ilc.allibilli.info/images/untitled1pane3_1_.jpg\" Border=\"0\" Height=\"1\" Width=\"368\" Name=\"Untitled12\" Alt=\"\"></Td><Td Width=\"19\" Height=\"1\"><Img Src=\"http://ilc.allibilli.info/images/untitled1pane4_1_.jpg\" Border=\"0\" Height=\"1\" Width=\"19\" Name=\"Untitled13\" Alt=\"\"></Td></Tr><Tr><Td Width=\"557\" Height=\"22\" ColSpan=\"4\"><Img Src=\"http://ilc.allibilli.info/images/untitled1pane1_2_.jpg\" Border=\"0\" Height=\"22\" Width=\"557\" Name=\"Untitled14\" Alt=\"\"></Td></Tr><Tr><Td Width=\"170\" Height=\"30\" ColSpan=\"2\"><Img Src=\"http://ilc.allibilli.info/images/untitled1pane1_3_.jpg\" Border=\"0\" Height=\"40\" Width=\"170\" Name=\"Untitled15\" Alt=\"\"></Td><Td Width=\"368\" Height=\"10\" bgcolor=\"#808080\"><p align=\"center\"><font color=\"#FFFFFF\" size=\"1\">");
		sb.append("ILC - Message");
		sb.append("</font></Td><Td Width=\"19\" Height=\"20\"><Img Src=\"http://ilc.allibilli.info/images/untitled1pane4_3_.jpg\" Border=\"0\" Height=\"40\" Width=\"19\" Name=\"Untitled17\" Alt=\"\"></Td></Tr><Tr><Td Width=\"557\" Height=\"6\" ColSpan=\"4\"><Img Src=\"http://ilc.allibilli.info/images/untitled1pane1_4_.jpg\" Border=\"0\" Height=\"6\" Width=\"557\" Name=\"Untitled18\" Alt=\"\"></Td></Tr><Tr><Td Width=\"18\" Height=\"274\"><Img Src=\"http://ilc.allibilli.info/images/untitled1pane1_5_.jpg\" Border=\"0\" Height=\"274\" Width=\"18\" Name=\"Untitled19\" Alt=\"\"></Td><Td Width=\"520\" Height=\"274\" ColSpan=\"2\"><p align=\"center\">");
		sb.append(emailDetails.getEmailContent().toString());
		sb.append("</Td><Td Width=\"19\" Height=\"274\"><Img Src=\"http://ilc.allibilli.info/images/untitled1pane4_5_.jpg\" Border=\"0\" Height=\"274\" Width=\"19\" Name=\"Untitled111\" Alt=\"\"></Td></Tr><Tr><Td Width=\"557\" Height=\"29\" ColSpan=\"4\"><Img Src=\"http://ilc.allibilli.info/images/untitled1pane1_6_.jpg\" Border=\"0\" Height=\"29\" Width=\"557\" Name=\"Untitled112\" Alt=\"\"></Td></Tr></Table></Td></Tr><Tr><Td><Img Src=\"http://ilc.allibilli.info/images/is_single_pixel_gif.gif\" Alt=\"\" Width=\"19\" Height=\"1\"></Td><Td><Img Src=\"http://ilc.allibilli.info/images/is_single_pixel_gif.gif\" Alt=\"\" Width=\"557\" Height=\"1\"></Td></Tr></Table>");
		log.info(sb.toString());
		return sb.toString();
		
	}
	/**
	 * @return the mailTransportProtocol
	 */
	public  String getMailTransportProtocol() {
		return mailTransportProtocol;
	}
	/**
	 * @param mailTransportProtocol the mailTransportProtocol to set
	 */
	public  void setMailTransportProtocol(String mailTransportProtocol) {
		this.mailTransportProtocol = mailTransportProtocol;
	}
	/**
	 * @return the mailSmtpAuth
	 */
	public  String getMailSmtpAuth() {
		return mailSmtpAuth;
	}
	
	/**
	 * @param mailSmtpAuth the mailSmtpAuth to set
	 */
	public  void setMailSmtpAuth(String mailSmtpAuth) {
		this.mailSmtpAuth = mailSmtpAuth;
	}
	/**
	 * @return the mailUser
	 */
	public  String getMailUser() {
		return mailUser;
	}
	/**
	 * @param mailUser the mailUser to set
	 */
	public  void setMailUser(String mailUser) {
		this.mailUser = mailUser;
	}
	/**
	 * @return the mailPassword
	 */
	public  String getMailPassword() {
		return mailPassword;
	}
	/**
	 * @param mailPassword the mailPassword to set
	 */
	public  void setMailPassword(String mailPassword) {
		this.mailPassword = mailPassword;
	}
	/**
	 * @return the mailSmtpHost
	 */
	public  String getMailSmtpHost() {
		return mailSmtpHost;
	}
	/**
	 * @param mailSmtpHost the mailSmtpHost to set
	 */
	public  void setMailSmtpHost(String mailSmtpHost) {
		this.mailSmtpHost = mailSmtpHost;
	}
	/**
	 * @return the mailFrom
	 */
	public  String getMailFrom() {
		return mailFrom;
	}
	/**
	 * @param mailFrom the mailFrom to set
	 */
	public  void setMailFrom(String mailFrom) {
		this.mailFrom = mailFrom;
	}
	/**
	 * @return the mailBcc
	 */
	public  String getMailBcc() {
		return mailBcc;
	}
	/**
	 * @param mailBcc the mailBcc to set
	 */
	public  void setMailBcc(String mailBcc) {
		this.mailBcc = mailBcc;
	}
	/**
	 * @return the mailSubject
	 */
	public  String getMailSubject() {
		return mailSubject;
	}
	/**
	 * @param mailSubject the mailSubject to set
	 */
	public  void setMailSubject(String mailSubject) {
		this.mailSubject = mailSubject;
	}
	/**
	 * @return the mailMessage
	 */
	public  String getMailMessage() {
		return mailMessage;
	}
	/**
	 * @param mailMessage the mailMessage to set
	 */
	public  void setMailMessage(String mailMessage) {
		this.mailMessage = mailMessage;
	}
	/**
	 * @return the mailPort
	 */
	public  String getMailPort() {
		return mailPort;
	}
	/**
	 * @param mailPort the mailPort to set
	 */
	public  void setMailPort(String mailPort) {
		this.mailPort = mailPort;
	}
	/**
	 * @return the mailDebug
	 */
	public  boolean isMailDebug() {
		return mailDebug;
	}
	/**
	 * @param mailDebug the mailDebug to set
	 */
	public  void setMailDebug(boolean mailDebug) {
		this.mailDebug = mailDebug;
	}
}
